{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "06c307c1",
   "metadata": {},
   "outputs": [],
   "source": [
    ":dep jsonpath-rust = \"=0.5.0\"\n",
    ":dep serde_json = \"1.0.25\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03f5e11a",
   "metadata": {},
   "source": [
    "### JsonPath 示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5a360064",
   "metadata": {},
   "outputs": [],
   "source": [
    "use jsonpath_rust::JsonPathFinder;\n",
    "use jsonpath_rust::JsonPathValue;\n",
    "use serde_json::{json,Value};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "03be17bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Some(1)"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "let finder = JsonPathFinder::from_str(r#\"{\"first\":{\"second\":[{\"active\":1},{\"passive\":1}]}}\"#, \"$.first.second[?(@.active)]\").unwrap();\n",
    "\n",
    "finder.find_slice()[0].clone().to_data()[\"active\"].as_i64()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "ce69a3c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "let json2 = r#\"{\"first\":{\"second\":[{\"active\":2},{\"passive\":1}]}}\"#;\n",
    "let path2 = \"$..active\";\n",
    "let finder = JsonPathFinder::from_str(json2, path2).unwrap();\n",
    "finder.find_slice()[0].clone().to_data().as_i64().unwrap()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1bdddc9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "pub fn json_extract(json: &str, json_path: &str) -> Value {\n",
    "    let finder = JsonPathFinder::from_str(json, json_path).unwrap();\n",
    "    let slice_of_data:Vec<JsonPathValue<Value>> = finder.find_slice();\n",
    "    let result: Vec<Value> = slice_of_data.iter().map(|item| item.clone().to_data()).collect();\n",
    "    result.first().unwrap().clone()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "94444b6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "let json = r#\"{\"first\":{\"second\":[{\"active\":2},{\"passive\":1}]}}\"#;\n",
    "let json_path = \"$.first.second[?(@.active)]\";\n",
    "\n",
    "let data = json_extract(json, json_path);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "067e0de2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Object {\"active\": Number(2)}]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d312fee2",
   "metadata": {},
   "source": [
    "### Json 示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "229b959d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Object {\"first\": Object {\"second\": Array [Object {\"active\": Number(888)}, Object {\"passive\": Number(1)}]}}"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "let active = 888;\n",
    "json!({\"first\":{\"second\":[{\"active\":active},{\"passive\":1}]}})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf2f1622",
   "metadata": {},
   "source": [
    "### 封装和转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5e7ed026",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Array [Object {\"age\": Number(30), \"name\": String(\"A\")}, Object {\"age\": Number(31), \"name\": String(\"B\")}, Object {\"age\": Number(32), \"name\": String(\"C\")}, Object {\"age\": Number(33), \"name\": String(\"D\")}]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "let json3 = r#\"\n",
    "{\n",
    "    \"name\": \"lili\",\n",
    "    \"age\": 18,\n",
    "    \"teachers\": [\n",
    "        {\"name\": \"A\", \"age\": 30},\n",
    "        {\"name\": \"B\", \"age\": 31},\n",
    "        {\"name\": \"C\", \"age\": 32},\n",
    "        {\"name\": \"D\", \"age\": 33}\n",
    "    ]\n",
    "}\"#;\n",
    "\n",
    "let result = json_extract(&json3, \"$..teachers\");\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b636790d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"data\":[{\"age\":30,\"name\":\"A\"},{\"age\":31,\"name\":\"B\"},{\"age\":32,\"name\":\"C\"},{\"age\":33,\"name\":\"D\"}]}\n",
      "[{\"age\":30,\"name\":\"A\"},{\"age\":31,\"name\":\"B\"},{\"age\":32,\"name\":\"C\"},{\"age\":33,\"name\":\"D\"}]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "let json4 = json!({\"data\": result});\n",
    "\n",
    "println!(\"{}\", json4);\n",
    "\n",
    "println!(\"{}\", json4[\"data\"].to_string());"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "035aa62d",
   "metadata": {},
   "outputs": [
    {
     "ename": "Error",
     "evalue": "invalid left-hand side of assignment",
     "output_type": "error",
     "traceback": [
      "        data.get_mut(\"age\").unwrap().as_i64().unwrap() = 25;",
      "\u001b[91m        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u001b[0m \u001b[94mcannot assign to this expression\u001b[0m",
      "        data.get_mut(\"age\").unwrap().as_i64().unwrap() = 25;",
      "\u001b[91m                                                       ^\u001b[0m \u001b[94merror: invalid left-hand side of assignment\u001b[0m",
      "invalid left-hand side of assignment"
     ]
    }
   ],
   "source": [
    "fn ttt2() {\n",
    "    let json4 = r#\"\n",
    "    [\n",
    "        {\n",
    "            \"name\": \"lili\",\n",
    "            \"age\": 18,\n",
    "            \"teachers\": [\n",
    "                {\"name\": \"A\", \"course\": 30},\n",
    "                {\"name\": \"B\", \"course\": 31},\n",
    "                {\"name\": \"C\", \"course\": 32},\n",
    "                {\"name\": \"D\", \"course\": 33}\n",
    "            ]\n",
    "        },\n",
    "        {\n",
    "            \"name\": \"apple\",\n",
    "            \"age\": 20,\n",
    "            \"teachers\": [\n",
    "                {\"name\": \"F\", \"course\": 30},\n",
    "                {\"name\": \"G\", \"course\": 31},\n",
    "                {\"name\": \"H\", \"course\": 32},\n",
    "                {\"name\": \"I\", \"course\": 33}\n",
    "            ]\n",
    "        }\n",
    "    ]\"#;\n",
    "\n",
    "    let value: Value = serde_json::from_str(&json4).unwrap();\n",
    "\n",
    "    // 修改所有 age 的值\n",
    "    let array = value.as_array().unwrap();\n",
    "    for item in array.iter() {\n",
    "        let data = item.as_object().unwrap();\n",
    "        data.get_mut(\"age\").unwrap().as_i64().unwrap() = 25;\n",
    "    }\n",
    "\n",
    "    // 打印修改后的 JSON\n",
    "    println!(\"{}\", serde_json::to_string(&value).unwrap());\n",
    "}\n",
    "\n",
    "ttt2()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Rust",
   "language": "rust",
   "name": "rust"
  },
  "language_info": {
   "codemirror_mode": "rust",
   "file_extension": ".rs",
   "mimetype": "text/rust",
   "name": "Rust",
   "pygment_lexer": "rust",
   "version": ""
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
